﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int a, b, c, d;
        int status;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (RadioButton1.IsChecked == true && RadioButton2.IsChecked == false)
            {
                status = 0;
            }
            else if (RadioButton1.IsChecked == false && RadioButton2.IsChecked == true)
            {
                status = 1;
            }
            else {
                status = 1; 
            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox2.Text.Length != 0)
            {
                a = Convert.ToInt32(TextBox2.Text.ToString());
            }
            else { a = 0; }
            if (TextBox3.Text.Length != 0)
            {
                b = Convert.ToInt32(TextBox3.Text.ToString());
            }
            else { b = 0; }
            if (TextBox4.Text.Length != 0)
            {
                c = Convert.ToInt32(TextBox4.Text.ToString());
            }
            else { c = 0; }
            Random r = new Random();
            c = r.Next(a, b + 1);
            TextBox4.Text = c.ToString();
            ListBox1.Items.Add(c);
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox2.Text.Length != 0)
            {
                a = Convert.ToInt32(TextBox2.Text.ToString());
            }
            else { a = 0; }
            if (TextBox3.Text.Length != 0)
            {
                b = Convert.ToInt32(TextBox3.Text.ToString());
            }
            else { b = 0; }
            if (TextBox4.Text.Length != 0)
            {
                c = Convert.ToInt32(TextBox4.Text.ToString());
            }
            else { c = 0; }
            if (TextBox5.Text.Length != 0)
            {
                d = Convert.ToInt32(TextBox5.Text.ToString());
            }
            else { d = 1; }
            if (d > (b + 1 - a))
            {
                d = (b + 1 - a);
            }
            Random r = new Random();
            List<int> Result = new List<int>();
            if (status == 1)
            {
                while (Result.Count < d)
                {
                    c = r.Next(a, b + 1);
                    if (!Result.Contains(c))
                    {
                        Result.Add(c);
                        ListBox1.Items.Add(c);
                    }
                }
            }
            else if (status == 0) {
                while (Result.Count < d)
                {
                    c = r.Next(a, b + 1);
                    Result.Add(c);
                    ListBox1.Items.Add(c);
                }
            }
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            ListBox1.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("", System.ComponentModel.ListSortDirection.Ascending));
        }

        private void Button5_Click(object sender, RoutedEventArgs e)
        {
            ListBox1.Items.Clear();
        }

        private void Button6_Click(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
            List<int> Result = new List<int>();
                while (Result.Count < 6)
                {
                    c = r.Next(1, 33 + 1);
                    if (!Result.Contains(c))
                    {
                        Result.Add(c);
                        ListBox1.Items.Add(c);
                    }
                }
            ListBox1.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("", System.ComponentModel.ListSortDirection.Ascending));
            c= r.Next(1, 16 + 1);
            ListBox1.Items.Add(c);
        }

        private void Button7_Click(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
            List<int> Result = new List<int>();
            while (Result.Count < 5)
            {
                c = r.Next(1, 35 + 1);
                if (!Result.Contains(c))
                {
                    Result.Add(c);
                    ListBox1.Items.Add(c);
                }
            }
            ListBox1.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("", System.ComponentModel.ListSortDirection.Ascending));
            c = r.Next(1, 12 + 1);
            Result.Add(c);
            ListBox1.Items.Add(c);
            while (Result.Count < 7) { 
                d = r.Next(1, 12 + 1);
                if (d != c) {
                    Result.Add(d);
                    ListBox1.Items.Add(d);
                }
            }
        }

    }
}
